// 列表按照父子转换成树
function getTree(data) {
	let result = [];
	let map = {};
	data.forEach(item => {
		map[item.id] = item;
	});
	data.forEach(item => {
		let parent = map[item.parentId];
		if (parent) {
			(parent.children || (parent.children = [])).push(item);
		} else {
			result.push(item);
		}
	});
	return result;
}

async function build_commentList(article_id) {
	console.log(article_id)
	let res =  {
		"comment": getTree(await get_comment_list(article_id))
	}
	return res
}

function like(res) {
	console.log(res)
}

async function add_comment(comment, article_id) {
	console.log(comment)
	let res = await uni.request({
		header: {"token": getApp().globalData.access_token },
		url: getApp().globalData.base_url + "/add/post_comments/" + article_id,
		data: {
			content: comment.content,
			pid: comment.pid
		},
		method: "POST"
	})
	return {
		"comment": getTree(res.data)
	}
}

async function get_comment_list(id) {
	console.log("请求了后段评论")
	let data = await uni.request({
		url: getApp().globalData.base_url + "/get/post_comments/" + id
	})
	return data.data
}

function handle(turl, id, title) {
	uni.redirectTo({url: "/pages/artical/artical?url="+turl+"&article_id="+id+"&title="+title})
}


module.exports.build_commentList = build_commentList
module.exports.handle = handle
module.exports.like = like
module.exports.add_comment = add_comment